-
-
Notifications
You must be signed in to change notification settings - Fork 82
feat: Introduce stacBadge widget and parser with example JSON #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
- Added StacBadge widget to represent notifications and counts. - Implemented StacBadgeParser for parsing badge JSON configurations. - Created badge_example.json to demonstrate badge usage in the gallery. - Updated home_screen.json to include a navigation tile for the badge example. - Registered StacBadgeParser in the StacService for widget parsing.
WalkthroughAdds Badge support: a StacBadge model with JSON (de)serialization, a StacBadgeParser and registration in StacService, WidgetType enum extension, gallery example and home entry, parser export, and documentation. Changes
Sequence Diagram(s)sequenceDiagram
participant Gallery as Gallery JSON
participant Service as StacService
participant Parser as StacBadgeParser
participant Flutter as Flutter Badge Widget
Gallery->>Service: load JSON asset (`badge_example.json`)
Service->>Parser: find parser for type "badge" and pass model JSON
Parser->>Parser: getModel() -> StacBadge.fromJson
Parser->>Parser: apply count/maxCount/label resolution & map props
Parser->>Flutter: return constructed Badge widget (child/label/colors/sizes)
Flutter-->>Gallery: rendered badge UI
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@divyanshub024 hi can you please review this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart (1)
21-23: Consider validation instead of assertions for production safety.The assertions validate count and maxCount, but assertions are disabled in production/release builds. While this matches Flutter's Badge widget pattern, invalid values could pass through silently in production.
Consider adding explicit validation:
- assert(model.count! >= 0, 'count must be non-negative'); + if (model.count! < 0) { + throw ArgumentError('count must be non-negative'); + } final maxCount = model.maxCount ?? 999; - assert(maxCount > 0, 'maxCount must be positive'); + if (maxCount <= 0) { + throw ArgumentError('maxCount must be positive'); + }packages/stac_core/lib/widgets/badge/stac_badge.dart (2)
127-147: Enforce or centralizecount >= 0andmaxCount > 0constraints.The docs state that
countmust be non‑negative andmaxCountmust be positive, but the model itself doesn’t enforce this. If validation currently lives only inStacBadgeParser, consider either:
- Adding simple assertions in the constructor, or
- Clearly documenting in this file that validation is performed at the parser layer.
This keeps behavior predictable if other consumers instantiate
StacBadgedirectly.
81-95: Align JSON deserialization defaults formaxCount/isLabelVisiblewith documented values.You document
maxCountas defaulting to999andisLabelVisibleas effectively default‑true, and you set those as constructor defaults. Depending on yourjson_serializableconfiguration, objects built viafromJsonmay still see these asnullwhen the keys are absent, pushing defaulting logic into the parser.To keep defaults declarative at the model level and reduce duplication in parsers, consider adding explicit
@JsonKey(defaultValue: ...)annotations, e.g.:@@ - /// Maximum count value before showing '[maxCount]+' format. - /// - /// Only used when [count] is provided. Defaults to 999. - /// Must be positive (> 0). - final int? maxCount; + /// Maximum count value before showing '[maxCount]+' format. + /// + /// Only used when [count] is provided. Defaults to 999. + /// Must be positive (> 0). + @JsonKey(defaultValue: 999) + final int? maxCount; @@ - /// If false, the badge's [label] is not included. - final bool? isLabelVisible; + /// If false, the badge's [label] is not included. + @JsonKey(defaultValue: true) + final bool? isLabelVisible;This keeps model semantics consistent whether instances are created directly or via JSON.
Also applies to: 140-147
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
examples/stac_gallery/assets/json/badge_example.json(1 hunks)examples/stac_gallery/assets/json/home_screen.json(2 hunks)packages/stac/lib/src/framework/stac_service.dart(1 hunks)packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart(1 hunks)packages/stac/lib/src/parsers/widgets/widgets.dart(1 hunks)packages/stac_core/lib/foundation/specifications/widget_type.dart(1 hunks)packages/stac_core/lib/widgets/badge/stac_badge.dart(1 hunks)packages/stac_core/lib/widgets/badge/stac_badge.g.dart(1 hunks)packages/stac_core/lib/widgets/widgets.dart(1 hunks)
🔇 Additional comments (10)
packages/stac_core/lib/widgets/badge/stac_badge.g.dart (1)
1-54: LGTM! Generated serialization code.This is auto-generated JSON serialization code for StacBadge. The default values (maxCount: 999, isLabelVisible: true) align with Flutter's Badge widget behavior.
packages/stac/lib/src/framework/stac_service.dart (1)
101-101: LGTM! Badge parser registered correctly.The StacBadgeParser is properly registered in the parsers list, following the established pattern.
packages/stac_core/lib/widgets/widgets.dart (1)
9-9: LGTM! Badge widget export added correctly.The export is properly placed in alphabetical order and follows the established pattern.
packages/stac_core/lib/foundation/specifications/widget_type.dart (1)
24-25: LGTM! Badge enum member added correctly.The badge widget type is properly documented and alphabetically ordered within the WidgetType enum.
examples/stac_gallery/assets/json/home_screen.json (1)
48-69: LGTM! Badge navigation tile added correctly.The list tile for Stac Badge follows the established pattern and correctly navigates to the badge example JSON asset.
packages/stac/lib/src/parsers/widgets/widgets.dart (1)
6-6: LGTM! Badge parser export added correctly.The export is properly placed in alphabetical order.
examples/stac_gallery/assets/json/badge_example.json (1)
1-201: LGTM! Comprehensive badge examples.This example JSON demonstrates various badge configurations effectively:
- Label-based badges (text and numeric)
- Count-based badges with maxCount behavior
- Small badges for notification dots
- Badge wrapping interactive widgets
The examples align well with Flutter's Badge widget capabilities.
packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart (2)
26-29: LGTM! Count-to-label conversion logic is correct.The implementation properly handles maxCount clamping and label generation, matching Flutter's Badge.count behavior:
- Shows actual count when ≤ maxCount
- Shows "maxCount+" when count exceeds maxCount
35-47: LGTM! Badge widget construction is correct.All properties are properly mapped from the StacBadge model to Flutter's Badge widget:
- Color conversions handled correctly
- Size properties passed through
- Style and layout properties parsed appropriately
- Label and child resolved from model
packages/stac_core/lib/widgets/badge/stac_badge.dart (1)
7-70: Docs and examples forStacBadgeare clear and aligned with FlutterBadge.The top-level documentation plus Dart/JSON snippets mirror Flutter’s
Badgesemantics and should make both direct Dart usage and server‑driven JSON configuration straightforward. Nice coverage oflabelvscount/maxCountbehavior.
Potatomonsta
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kumar-gautam24
Overall looks good! A couple of minor items:
-
Missing
Badge.countconvenience constructor: Consider adding aStacBadge.count()factory constructor for better API parity with Flutter'sBadge.count(). -
Badge icon button example issues
- There seems to be some additional padding on the Badge IconButton example
- Add
"onPressed": { "type": "none" }to the iconButton to enable tap effects
Otherwise solid implementation! 👍
|
@kumar-gautam24
This will ensure the badge widget is properly documented for users. 😊 |
|
Sure will do these and update |
…rations - Created badge.mdx to document the StacBadge widget, including its properties and usage. - Updated badge_example.json to include padding and onPressed properties for badge examples. - Enhanced StacBadge class with a convenience constructor for numeric labels based on count.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
docs/widgets/badge.mdx (2)
21-23: Clarifylabelvscountinteraction and count constraints in docsThe properties table doesn’t currently call out that when
countis provided,labelis ignored, and thatcountmust be non‑negative andmaxCountpositive (as enforced/assumed in the Dart model and Flutter’sBadge.count). Consider expanding thelabel,count, andmaxCountrows to spell this out so JSON authors don’t end up with surprising behavior or invalid values.
136-143: VerifyalignmentJSON shape matchesStacAlignmentGeometryexpectationsThe custom styling example uses:
"alignment": { "dx": 1.0, "dy": -1.0 }Please double‑check this matches how
StacAlignmentGeometryis documented elsewhere (some APIs usex/yinstead ofdx/dy). If the canonical shape differs, updating this example will prevent confusion for users copying it.packages/stac_core/lib/widgets/badge/stac_badge.dart (1)
78-81: Consider centralizingcount/maxCountvalidation beyond the.countfactoryRight now, invariants for
countandmaxCount(non‑negativecount, positivemaxCount) are only enforced via asserts inStacBadge.count. Instances created via the main constructor orfromJsoncan carry invalid values through to the FlutterBadge, which will then assert at build time.To make behavior more predictable for server‑driven JSON:
- Either move the
count >= 0/maxCount > 0checks into the primary constructor (or a shared private initializer) so all code paths enforce them, or- Add explicit validation in the parser / JSON layer and document how invalid values are handled (e.g., clamped or falling back to defaults).
This isn’t a blocker, but tightening this up would give earlier and clearer feedback when JSON is misconfigured.
Also applies to: 97-105, 121-151, 183-200
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
docs/widgets/badge.mdx(1 hunks)examples/stac_gallery/assets/json/badge_example.json(1 hunks)packages/stac_core/lib/widgets/badge/stac_badge.dart(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- examples/stac_gallery/assets/json/badge_example.json
🔇 Additional comments (1)
packages/stac_core/lib/widgets/badge/stac_badge.dart (1)
7-70: Good, well‑documented model aligned with Flutter’sBadgeThe class‑level docs, Dart snippets, and JSON snippets closely mirror Flutter’s
Badgesemantics and the JSON format exposed in the MDX docs. The addedtypegetter andfromJson/toJsonwiring look consistent with other Stac widgets.
|
@Potatomonsta hi i have added the factory constructor for the Badge.count() can you , review it. |
Description
Add support for Badge widget in Stac.
Changes Made
Core Implementation:
badgetoWidgetTypeenumStacBadgemodel class with all Flutter Badge properties (includingcount/maxCountfor Badge.count() support)StacBadgeParserto convert JSON → Flutter Badge widgetStacServiceFiles:
packages/stac_core/lib/widgets/badge/stac_badge.dart(new)packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart(new)packages/stac_core/lib/foundation/specifications/widget_type.dart(modified)packages/stac/lib/src/framework/stac_service.dart(modified)examples/stac_gallery/assets/json/badge_example.json(new)examples/stac_gallery/assets/json/home_screen.json(modified)Features
countandmaxCountpropertiesRelated Issues
Closes #384
Type of Change
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.